package day3;

import org.junit.Test;

import java.util.Collections;

public class ArrayDemo {

    @Test
    public void test1() {
        int score1 = 78;
        int score2 = 58;
        int score3 = 98;
        int sum = score1 + score2 + score3;

        // array:变量  new出来的也叫：对象
        int[] array = new int[4];
        System.out.println(array.length);// 4
        array[0] = 12;
        array[1] = 4;
        array[2] = 54;
        array[3] = 89;
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        System.out.println("--------------");
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.println(array[i]);
        }
    }

    @Test
    public void test2() {
        // 元素类型[] 数组名 = new 元素类型[长度];
        int[] array1 = new int[3];
        for (int i = 0; i < array1.length; i++) {
            System.out.println(array1[i]);
        }
        double[] array2 = new double[3];
        boolean[] array3 = new boolean[3];
        char[] array4 = new char[3];
        // Student[] array5 = new Student[3];

        int[] array6 = {34, 45, 6, 7};
        int[] array7 = new int[]{34, 45, 6, 7};
    }

    @Test
    public void test55() {
        int[] array = new int[4];
        array[0] = 12;
        array[1] = 4;
        array[2] = 54;
        array[3] = 89;
        for (int i = 0; i <= array.length; i++) {
            System.out.println(array[i]);
            // 数组下标越界异常
            // java.lang.Array Index OutOf Bounds Exception: 4
            // array[4]
        }
    }

    @Test
    public void test54() {
        int[] array = new int[4];
        array[0] = 12;
        array[1] = 4;
        array[2] = 54;
        array[3] = 89;
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
            if (max < array[i]) {
                max = array[i];
            }
        }

        System.out.println("max: " + max);
    }

    @Test
    public void testBubbleSort() {
        int[] array = {40, 17, 21, 1};
        for (int i = 1; i <= array.length - 1; i++) {
            // i=1,就是把最大的40冒到最右边  j:3
            // i=2,就是把剩下最大的21冒到最右边  j:2
            // i=3,就是把剩下最大的17冒到最右边  j:1
            // i + j = array.length
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }

        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    @Test
    public void testBubbleSort1() {
        int[] array = {40, 17, 21, 1};
        for (int i = 0; i < array.length - 1; i++) {
            // i=0,就是把最大的40冒到最右边  j:3
            // i=1,就是把剩下最大的21冒到最右边  j:2
            // i=2,就是把剩下最大的17冒到最右边  j:1
            // i + j = array.length-1
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }

        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}
